﻿namespace FiftyOne.Foundation.Mobile.Detection
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;

    internal class Strings
    {
        private static Dictionary<int, object> _index = new Dictionary<int, object>();
        private static List<string> _values = new List<string>();

        internal static int Add(string value)
        {
            int index = 0;
            int hashCode = value.GetHashCode();
            lock (_index)
            {
                index = IndexOf(value, hashCode);
                if (index == -1)
                {
                    index = AddValue(value);
                    _index.Add(hashCode, index);
                }
                else if (_values[index] != value)
                {
                    List<int> list = null;
                    object obj2 = _index[hashCode];
                    if (obj2 is int)
                    {
                        list = new List<int>();
                        list.Add((int) obj2);
                    }
                    else
                    {
                        list = new List<int>((int[]) obj2);
                    }
                    index = AddValue(value);
                    list.Add(index);
                    _index[hashCode] = list.ToArray();
                }
            }
            Debug.Assert((index >= 0) && (index < _values.Count), "Result is out of value range.");
            Debug.Assert(index == _values.IndexOf(value), "New value was not found at correct position in the list.");
            Debug.Assert(IndexOf(value) == index, "Index does not return matching string position.");
            return index;
        }

        private static int AddValue(string value)
        {
            lock (_values)
            {
                _values.Add(value);
                return _values.IndexOf(value);
            }
        }

        internal static bool Contains(string value)
        {
            return (IndexOf(value) >= 0);
        }

        internal static string Get(int index)
        {
            return _values[index];
        }

        internal static int IndexOf(string value)
        {
            return IndexOf(value, value.GetHashCode());
        }

        internal static int IndexOf(string value, int hashcode)
        {
            object obj2 = null;
            if (_index.TryGetValue(hashcode, out obj2))
            {
                if (obj2 is int)
                {
                    return (int) obj2;
                }
                int[] numArray = (int[]) obj2;
                if (numArray.Length == 1)
                {
                    return numArray[0];
                }
                foreach (int num in numArray)
                {
                    if (_values[num] == value)
                    {
                        return num;
                    }
                }
            }
            return -1;
        }

        internal static int Count
        {
            get
            {
                return _values.Count;
            }
        }
    }
}

